#include<bits/stdc++.h>

using namespace std;

string path , SSS;
struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Tree {
public:
    string path , SSS;
    void dfs_s(TreeNode* r)
    {
        if(!r)
        {
            path += "#,";
        }
        else
        {
            int val = r->val;
            path += to_string(val) + ",";
            dfs_s(r->left);
            dfs_s(r->right);
        }
    }
    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        dfs_s(root);
        cout<<path<<endl;
        return path;
    }
    TreeNode* dfs_d(int& u)
    {
        if(u >= SSS.size()) return NULL;
        if(SSS[u] == '#')
        {
            u += 2;
            return NULL;
        }
        //求当前值
        int k = u;
        while(SSS[k] != ',') ++k;
        int val = stoi(SSS.substr(u , k - u));
        TreeNode* r = new TreeNode(val);
        //跳过逗号
        u = k + 1;
        r->left = dfs_d(u);
        r->right = dfs_d(u);
        return r;
    }
    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        // return NULL;
        int u = 0;
        SSS = data;
        return dfs_d(u);
    }
};

void dfs_pre(TreeNode* r)
{
    if(!r) return;
    cout<<r->val<<" ";
    dfs_pre(r->left);
    dfs_pre(r->right);
}

int main()
{
    string s = "1,2,4,#,#,5,#,#,3,6,#,#,7,#,#,";
    Tree tr;
    TreeNode* r = tr.deserialize(s);
    dfs_pre(r);
    cout<<endl;
    cout<<tr.serialize(r)<<endl;
    system("pause");
    return 0;
}